#include "chatlistwidget.h"
#include "ui_chatlistwidget.h"
#include "chatlistitem.h"
#include <QVariant>

ChatListWidget::ChatListWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChatListWidget),m_item(Q_NULLPTR)
{
    ui->setupUi(this);
}

ChatListWidget::~ChatListWidget()
{
    delete ui;
}

void ChatListWidget::AddItem(Friend *data)
{
    auto item = new QListWidgetItem;
    QVariant var = QVariant::fromValue(data);
    item->setData(Qt::UserRole, var);
    item->setSizeHint(QSize(250, 65));
    ui->listWidget->addItem(item);

    auto widget = new ChatListItem;
    widget->item(data);
    ui->listWidget->setItemWidget(item, widget);

    connect(widget, &ChatListItem::selected, this, &ChatListWidget::onItemSelected);
}

void ChatListWidget::onItemSelected(ChatListItem *item)
{
    if (item == Q_NULLPTR)
    {
        return;
    }
    if (m_item != Q_NULLPTR)
    {
        m_item->Selected(false);
        m_item->Checked(false);
    }
    item->Selected(true);
    item->Checked(true);
    m_item = item;

    QVariant var = QVariant::fromValue(item->item());
    emit onItemClicked(var, PageType::AllChatView);
}
